package com.example.labb2da523b;

import java.sql.SQLException;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.labb2da523b.db.Expense;
import com.example.labb2da523b.db.SqlHelper;

public class MainActivity extends Activity {
	
	Integer amountint;
	Expense newExpense;
	
	Button saveButton;
	Button clearButton;
	EditText sumField;
	EditText nameField;
	EditText dateField;
	Spinner category;
	SqlHelper db = new SqlHelper(this);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ActionBar ab = getActionBar();
		ab.setDisplayShowCustomEnabled(true);
		ab.setDisplayShowTitleEnabled(false);
		ab.setCustomView(R.layout.title_theme);
		
		saveButton = (Button) findViewById(R.id.savebutton);
		clearButton = (Button) findViewById(R.id.clearbutton);

		sumField = (EditText) findViewById(R.id.amountedit);
		nameField = (EditText) findViewById(R.id.expenseedit);
		dateField = (EditText) findViewById(R.id.dateedit);
		category = (Spinner) findViewById(R.id.categoryspinner);
		
		saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				newExpense = new Expense();
				//Expense name
				newExpense.setName(nameField.getText().toString());
				//date
				newExpense.setDate(dateField.getText().toString());
				//category
				newExpense.setCategory(category.getSelectedItem().toString());
				//sum
				newExpense.setSum(Integer.parseInt(sumField.getText().toString()));

				addExpense();
				
				Intent intEnt = new Intent();
				intEnt.setClass(MainActivity.this, CircleDiagram.class);
				startActivity(intEnt);
			}		
		});
		
		clearButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				sumField.setText(null);
				nameField.setText(null);
				dateField.setText(null);
			}		
		});
	}
	
	public void addExpense(){
		try{
			db.open();
			long id = db.insertExpense(newExpense);
			db.close();
			Toast.makeText(MainActivity.this, "Added", Toast.LENGTH_LONG).show();
		}
		catch(SQLException e){
			e.printStackTrace();
			db.close();
		}
		finally{
			sumField.setText(null);
			nameField.setText(null);
			dateField.setText(null);
		}
	}
	public void addBudget(){
		try {
			db.open();
			long id = db.insertBudget(20);
			db.close();
		} catch (SQLException e) {
			e.printStackTrace();
			db.close();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuHelper.Menu(item, this);
	}
}
